/**
 * 获得搜索参数
 * @param params
 * @returns {Object}
 */
function getSearchParams(params) {
    var searchParams = new Object();
    if (params != undefined) {
        searchParams = params;
    }
    $('.SearchForm').each(function () {
        var param = $(this).val().trim();
        if (param == undefined)
            param = '';
        searchParams[$(this).attr('id')] = param;
    });
    return searchParams;
}

function getSearch() {
    var search = new Object();
    $('.SearchForm').each(function () {
        var param = $(this).val().trim();
        if (param == undefined)
            param = '';
        search[$(this).attr('id')] = param;
    });
    return search;
}

/**
 * 页面自加载
 */
$(function () {
    $('#time').datepicker({
        format : "yyyy-mm-dd",
        todayBtn : "linked",
        autoclose : true,
        todayHighlight : true,
        clearBtn : true
    });
    var cellwidth = ($(".box-content.table-responsive").width() - 55) / 11;
    var psval = $('#datatable_noticeinfo').attr('data-size');
    if (psval == undefined || psval == "") {
        psval = 10;
    }
    /**
     * easyui 生成表格数据
     */
    $('#datatable_noticeinfo').datagrid({
        striped: true,
        remoteSort: false,
        idField: 'n_id',
        collapsible: true,
        fit: false,
        url: 'admin/Project/getCourseJSON',//数据源路径
        loadMsg: '请等待数据载入....',
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        pageSize: psval,
        pageList: [psval, psval * 2, psval * 3, psval * 4, psval * 5],
        columns: [[
            {
                field: 'user_username',
                title: '昵称',
                align: 'center',
                width: cellwidth,
                sortable: true,
            }, {
                field: 'user_phone',
                title: '手机号码',
                align: 'center',
                width: cellwidth,
                sortable: true,
            },{
                field: 'team_member_role',
                title: '角色',
                align: 'center',
                width: cellwidth,
                sortable: true,
                formatter:function(value,row,index){
                    if(value==1){
                        return "<span style='color:red;font-weight: bold'>决策层</span>";
                    }else if(value==2){
                        return "<span style='color:green;font-weight: bold'>管理层</span>";
                    }else if(value==3){
                        return "<span style='color:#F17A02;font-weight: bold'>成员层</span>";
                    }else{
                        return "<span style='color:#F17A02;font-weight: bold'>客户层</span>";
                    }

                }
            },{
                field: 'up_user',
                title: '所属客户',
                align: 'center',
                width: cellwidth,
                sortable: true
            }
            ,{
                field: 'team_member_join_time',
                title: '加入时间',
                align: 'center',
                width: cellwidth,
                sortable: true
            }
        ]],
        onBeforeLoad: function (param) {
            param = getSearchParams(param);
        },
    });
//搜索
    $('#Search').click(function () {
        $('#datatable_noticeinfo').datagrid('reload');
    });
    //清空搜索条件
    $("#Reset").click(function() {
        $('.SearchForm').val('');
    });
//添加
    $('.addbook').click(function () {
        var content = "";
        $.ajax({
            url: 'admin/Project/addMember',
            type: 'POST',
            dataType: 'HTML',//返回的数据类型
            success: function (updatemodalhtml) {
                content = updatemodalhtml;
                //弹出添加框
                $.TeachDialog({
                    title: '添加成员管理',
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    otherButtons: ['保存'],
                    otherButtonStyles: ['btn-primary'],
                    clickButton: function (sender, modal, index) {
                        $.ajax({
                            url: 'admin/Project/addTeamMember',
                            data: $("#addForm").serialize(),
                            type: 'POST',
                            dataType: 'JSON',
                            success: function (returnData) {
                                if (returnData.status == 0) {
                                    // $.TeachDialog({
                                    //     content: returnData.msg
                                    // });
                                    layer.open({
                                        content: returnData.msg
                                    });
                                } else {
                                    modal.modal('hide');
                                    $.TeachDialog({
                                        content: returnData.msg
                                    });
                                    $('#datatable_noticeinfo').datagrid('reload');
                                }
                            },
                            error: function () {
                                $.TeachDialog({
                                    content: '系统异常，请联系管理员',
                                });
                            },

                        });

                    },
                });
            },
            error: function () {
                $.TeachDialog({
                    content: '系统异常，请联系管理员',
                });
                return;
            }
        });
    });
//导出
    $(".removebook").click(function () {
        var data = getSearch();
       $.ajax({
           url: 'admin/Project/excelDownload',
           type: 'POST',
           data:data,
           dataType: 'JSON',
           success:function (res) {
             if(res.status==1){
                 location.href =(res.msg);
             }
           }
       })
    })
});

//成员导入页面
$('.import').click(function () {
    var content = "";
    $.ajax({
        url: 'admin/Project/ImportUser',
        type: 'POST',
        dataType: 'HTML',//返回的数据类型
        success: function (updatemodalhtml) {
            content = updatemodalhtml;
            //弹出添加框
            $.TeachDialog({
                title: '添加成员管理',
                content: content,
                showCloseButton: true,
                showCloseButtonName: '关闭',
                otherButtons: ['保存'],
                otherButtonStyles: ['btn-primary'],
                clickButton: function (sender, modal, index) {
                    // var formData = new FormData();
                    // var name = $("#upfile").val();
                    // formData.append("file",$("#upfile")[0].files[0]);
                    // formData.append("name",name);
                    var formData = new FormData($("#addForm" )[0]);

                    $.ajax({
                        url: 'admin/Project/insertImportUser',
                        data: formData,
                        type: 'POST',
                        dataType: 'JSON',
                        // processData : false,
                        // // 告诉jQuery不要去设置Content-Type请求头
                        // contentType : false,
                        async: false,
                        cache: false,
                        contentType: false,
                        processData: false,
                        success: function (returnData) {
                            if (returnData.status == 0) {
                                $.TeachDialog({
                                    content: returnData.msg
                                });
                            } else {
                                modal.modal('hide');
                                $.TeachDialog({
                                    content: returnData.msg
                                });
                                $('#datatable_noticeinfo').datagrid('reload');
                            }
                        },
                        error: function () {
                            $.TeachDialog({
                                content: '系统异常，请联系管理员',
                            });
                        },

                    });

                },
            });
        },
        error: function () {
            $.TeachDialog({
                content: '系统异常，请联系管理员',
            });
            return;
        }
    });
});